package com.smartguard.service;

import com.smartguard.bean.DeviceEvent;
import com.smartguard.dao.BaseDAO;

public class RuleService extends BaseService{
	
	//添加or更新DeviceEvent
	public boolean insertRule(DeviceEvent de) {
		DeviceEvent tempDE = new DeviceEvent();
		tempDE.setDeviceHash(de.getDeviceHash());
		tempDE.setNextDeviceHash(de.getNextDeviceHash());
		BaseDAO baseDAO = this.getBaseDAO();
		boolean existed = baseDAO.findOne(tempDE)!=null?true:false;
		if (existed) {
			return baseDAO.updateOne(tempDE, de);
		} else {
			return baseDAO.insert(de);
		}
		
	}
	
	public boolean deleteRule (DeviceEvent de) {
		String deviceHash = de.getDeviceHash();
		DeviceEvent tempDE = new DeviceEvent();
		tempDE.setNextDeviceHash(deviceHash);
		BaseDAO baseDAO = this.getBaseDAO();
		boolean existed = baseDAO.findOne(tempDE)!=null?true:false;
		if (existed) {
			baseDAO.delete(tempDE);
			return true;
		} 
		return false;
	}
	
	public void modifyRule (DeviceEvent de) {
		insertRule(de);
	}
	
	public DeviceEvent findOneRule (DeviceEvent de) {
		if (de == null) return null;
		else return this.getBaseDAO().findOne(de);
	}

	public static void main(String[] args) {
		RuleService ruleService = new RuleService();
		DeviceEvent de = new DeviceEvent();
		de.setDeviceHash("h2");
		de.setEventType(1);
		de.setNextDeviceHash("h3");
		de.setNextEventType(2);
		ruleService.insertRule(de);
	}
}
